# 数据读取及基本处理
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
import week4advance_drawFigure
import week4advance_FEtrain
import week4advance_LineRegres
import week4advance_ridgeCV_visualization_weightCoef
import week4advance_lasso
# 读入数据
train = pd.read_csv("day.csv")
print(train.head())
train.info()
# 对数据值型特征，用常用统计量观察其分布
pd.set_option('display.max_columns', None)
print(train.describe())

week4advance_drawFigure.drawFigure(train)  # 绘制各种直方图和箱型图以及提琴图
week4advance_FEtrain.FEtrain(train)        # 特征工程

df_minmax = pd.read_csv("FE_day.csv")

# 通过观察前5行，了解数据每列（特征）的概况
#print(df_minmax.head())
# 从原始数据中分离输入特征x和输出y
y1 = df_minmax["cnt"]
X1 = df_minmax.drop(["cnt"], axis=1)

# 特征名称，用于后续显示权重系数对应的特征
feat_names1 = X1.columns
# 随机采样20%的数据构建测试样本，其余作为训练样本
X_train, X_test, y_train, y_test = train_test_split(X1, y1, random_state=33, test_size=0.2)
#print(X_train.shape)

lrCoef, y_train_pred_lr = week4advance_LineRegres.lineRegres(X_train, y_train, X_test, y_test)

f, ax = plt.subplots(figsize=(7, 5))
f.tight_layout()
ax.hist(y_train - y_train_pred_lr, bins=40, label='Residuals Linear', color='b', alpha=.5)
ax.set_title("Histogram of Residuals")
ax.legend(loc='best')

mse_mean, alphas, ridgealpha, ridgeCoef = week4advance_ridgeCV_visualization_weightCoef.visual_weightCoef(X_train, y_train, X_test,
                                                                                             y_test)
plt.figure(12)
plt.plot(np.log10(alphas), mse_mean.reshape(len(alphas), 1))
plt.xlabel('log(alpha)')
plt.ylabel('ridgemse')
print('ridgealpha is:', ridgealpha)

lassoMses, lassoAlpha, lassoCoef = week4advance_lasso.lassoTest(X_train, y_train, X_test, y_test)
print(lassoAlpha[-1])
plt.figure(13)
plt.plot(np.log10(lassoAlpha), lassoMses)
plt.xlabel('log(alpha)')
plt.ylabel('lassoMses')

print('lassoalpha is:', lassoAlpha)


# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性
fs = pd.DataFrame({"columns": list(feat_names1), "coef_lr": list(lrCoef.T), "coef_ridge": list(ridgeCoef.T),"coef_lasso":list((lassoCoef.T))})
print(fs.sort_values(by=['coef_lr'], ascending=False))
plt.show()